package mr.fileToFile;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class S1MMEMapper extends Mapper<LongWritable, Text, Text, Text> {
	public boolean isBlank(final String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if (Character.isWhitespace(str.charAt(i)) == false) {
				return false;
			}
		}
		return true;
	}

	private Text txt1 = new Text();
	private Text txt2 = new Text();

	@Override
	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
		String[] lines = value.toString().split("\\|");
		if (lines.length < 34)
			return;
		String imsi = lines[5];//
		if (isBlank(imsi))
			return;
		String mmeUeS1apId = lines[18];//
		if (isBlank(mmeUeS1apId))
			return;
		String cellId = lines[33];//
		if (isBlank(cellId))
			return;
		String bit = Integer.toBinaryString(Integer.valueOf(cellId));
		if (bit.length() < 19) {
			return;
		}
		String enodebId = String.valueOf(Integer.valueOf(bit.substring(0, bit.length() - 8), 2));
		txt1.set(mmeUeS1apId + "," + imsi);
		txt2.set(enodebId);
		context.write(txt1, txt2);
	}
}
